package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.AlreadyExistsException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;
    /**
     * 根据id查询套餐
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        //查询套餐菜品关系表
        List<SetmealDish> setmealDishes = setmealDishMapper.getListBySetmealId(id);

        //查询套餐表
        Setmeal setmeal = setmealMapper.getSetmealById(id);

        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);

        setmealVO.setSetmealDishes(setmealDishes);

        return setmealVO;
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void insert(SetmealDTO setmealDTO) {

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //先查询是否存在该套餐
        Setmeal setmealByName = setmealMapper.getSetmealByName(setmealDTO.getName());
        if (setmealByName != null && setmealByName.getName().equals(setmealDTO.getName())){
            throw new AlreadyExistsException(setmealDTO.getName()+ MessageConstant.ALREADY_EXISTS);
        }

        //根据id查询菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();

        List<Dish> dishes = dishMapper.selectByCategoryId(setmeal.getCategoryId());


        //新增套餐
        setmealMapper.insert(setmeal);

        Long setmealId = setmeal.getId();
        //新增套餐菜品数据

        for (SetmealDish setmealDish: setmealDishes) {
            setmealDish.setSetmealId(setmealId);
            setmealDishMapper.insert(setmealDish);
        }
    }
    /**
     * 分页查询套餐
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult getQueryPage(SetmealPageQueryDTO setmealPageQueryDTO) {

        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        Page<Setmeal> setmeals = setmealMapper.getQueryPage(setmealPageQueryDTO);

        return new PageResult(setmeals.getTotal(),setmeals.getResult());
    }

    /**
     * 批量删除
     * @param ids
     */
    @Override
    @Transactional
    public void deleteById(List<Long> ids) {

        //启售状态的套餐不能删除
        for (Long id : ids ) {
            Setmeal setmeal = setmealMapper.getSetmealById(id);
            if (setmeal.getStatus() == StatusConstant.ENABLE){
                throw new AlreadyExistsException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        for (Long id : ids) {
            //删除套餐表
            setmealMapper.deleteById(id);
            //删除套餐菜品关系表
            setmealDishMapper.deleteBySetmealId(id);
        }

    }

    /**
     * 修改套餐数据
     * @param setmealDTO
     * @return
     */
    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {

        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //修改套餐数据
        setmealMapper.update(setmeal);
        //新增套餐菜品关联表数据
        Long setmealId = setmealDTO.getId();
        //先删除套餐菜品关联数据
        setmealDishMapper.deleteBySetmealId(setmealDTO.getId());
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        for (SetmealDish s: setmealDishes) {
            s.setSetmealId(setmealId);

        }
        log.info("批量新增套餐菜品关系表...");
        setmealDishMapper.insertSetmeal(setmealDishes);
    }

    /**
     * 根据id修改status
     * @param status
     * @param id
     */
    @Override
    public void setStatusById(Integer status, Long id) {

        if (status == StatusConstant.ENABLE){
            //SELECT d.*  FROM dish d LEFT JOIN setmeal_dish s ON d.id = s.dish_id WHERE s.setmeal_id = 36
            List<Dish> dishes = dishMapper.getBySetmealId(id);
            if(dishes != null && dishes.size() > 0){
                dishes.forEach(dish -> {
                    if (StatusConstant.DISABLE == dish.getStatus()){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }

        Setmeal setmeal = Setmeal.builder()
                        .status(status)
                        .id(id)
                        .build();
        setmealMapper.update(setmeal);
    }
}
